package com.leetcode.dfs;

public class NumSmallerByFrequency {
    public int[] numSmallerByFrequency(String[] queries, String[] words) {
        int[] resArr = new int[queries.length];
        int[] queryCount = new int[queries.length];
        for (int i = 0; i < queries.length; i++) {
            queryCount[i] = maxCount(queries[i]);
        }
        int[] wordCount = new int[words.length];
        for (int i = 0; i < words.length; i++) {
            wordCount[i] = maxCount(words[i]);
        }
        for (int i = 0; i < queries.length; i++) {
            resArr[i] = countLessWords(queryCount[i], wordCount);
        }
        return resArr;
    }

    private int countLessWords(int val, int[] wordCount) {
        int res = 0;
        for (int i = 0; i < wordCount.length; i++) {
            if (val < wordCount[i]) {
                res++;
            }
        }
        return res;
    }

    private int maxCount(String str){
        int res = 0;
        int[] countArr = new int[26];
        for (int i = 0; i < str.length(); i++) {
            countArr[str.charAt(i) - 'a']++;
        }
        for (int val : countArr) {
            if (val != 0) {
                res = val;
                break;
            }
        }
        return res;
    }
}
